// pages/guang/guang.js
import { request } from '../../request/index.js';
var app = getApp();
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		homePic: '',
		miao: 1,
	},
	cliadv: function () {
		clearInterval(this.time);
		let user = wx.getStorageSync('user');
		var that = this;
		wx.login({
			success: function (res) {
				request({
					url: '/UserInfo/getCode',
					data: {
						code: res.code,
					},
				})
					.then((r) => {
						var openid = 'userInfo.openid';
						that.setData({
							[openid]: r.data,
						});
					})
					.then((re) => {
						request({
							url: '/UserInfo/getAllUserInfoByType',
							data: {
								openid: that.data.userInfo.openid,
							},
						}).then((rs) => {
							if (rs.data && rs.data.list && rs.data.list.length == 0) {
								wx.navigateTo({
									url: '/pages/index/index',
								});
							} else {
								app.globalData.userId = rs.data.list[0].userId;
								wx.setStorageSync('user', rs.data.list[0]);
								if (that.data.manufacturerId) {
									wx.navigateTo({
										url:
											'/pages/resourceDetail/resourceDetail?manufacturerId=' +
											that.data.manufacturerId,
									});
								} else if (that.data.resourcesId) {
									wx.navigateTo({
										url:
											'/pages/resourcesShow/resourcesShow?resourcesId=' +
											that.data.resourcesId,
									});
								} else {
									wx.switchTab({
										url: '/pages/index/index',
									});
								}
							}
						});
					});
			},
		});
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		const { manufacturerId } = options;
		const { resourcesId } = options;
		if (manufacturerId) {
			this.setData({
				manufacturerId: manufacturerId,
			});
		} else if (resourcesId) {
			this.setData({
				resourcesId: resourcesId,
			});
		}
		let that = this;
		let user = wx.getStorageSync('user');
		// request({
		//   url: "/Advertisement/getAdvertisementIdLogin"
		// }).then(res => {
		//   if(res.data.pictureUrl==null)
		//     that.data.miao=0
		//   else{
		//   this.setData({
		//     homePic: res.data.pictureUrl
		//   })
		//   }
		this.setData({
			homePic: '',
		});
		// 在进入页面时开始执行定时器，每秒执行一次miao-1操作。
		that.time = setInterval(function () {
			that.setData({
				miao: that.data.miao - 1,
			});
			// 当miao==0时，清除定时器clearInterval(this.time)（一定要清除定时器），然后自动跳转到首页
			if (that.data.miao <= 0) {
				clearInterval(that.time);
				wx.login({
					success: function (res) {
						request({
							url: '/UserInfo/getCode',
							data: {
								code: res.code,
							},
						})
							.then((r) => {
								console.log(r);
								var openid = 'userInfo.openid';
								that.setData({
									[openid]: r.data,
								});
							})
							.then((re) => {
								request({
									url: '/UserInfo/getAllUserInfoByType',
									data: {
										openid: that.data.userInfo.openid,
									},
								}).then((rs) => {
									if (rs.data && rs.data && rs.data.list.length == 0) {
										wx.switchTab({
											url: '/pages/index/index',
										});
									} else {
										app.globalData.userId =
											rs.data.list[0].userId;
										wx.setStorageSync(
											'user',
											rs.data.list[0]
										);
										if (options.manufacturerId) {
											wx.navigateTo({
												url:
													'/pages/resourceDetail/resourceDetail?manufacturerId=' +
													options.manufacturerId,
											});
										} else if (options.resourcesId) {
											wx.navigateTo({
												url:
													'/pages/resourcesShow/resourcesShow?resourcesId=' +
													options.resourcesId,
											});
										} else {
											wx.switchTab({
												url: '/pages/index/index',
											});
										}
									}
								});
							});
					},
				});
			}
		}, 1000);
		// });
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {},
});
